#!/usr/bin/env python

from httputil import HttpUtil
from config import logger

class Storage(HttpUtil):
  def __init__(self, prefixURL=None):
    HttpUtil.__init__(self, prefixURL)

  def addSecondaryStorage(self, url, zoneid=None):
    command = "addSecondaryStorage"
    logger.debug("command=" + command)
    args = {"url": url}
    if zoneid is not None:
      args["zoneid"] =  zoneid
    response = self.http_get(command, args=args)
    logger.debug(response)
    return response["secondarystorage"]

  def destroySecondaryStorage(self, secondarystorageid):
    command = "deleteHost"
    logger.debug("command=" + command)
    args = {"id": secondarystorageid}
    response = self.http_get(command, args=args)
    logger.debug(response)
    return response

  def listSecondaryStorages(self):
    command = "listHosts"
    logger.debug("command=" + command)
    args = {"type": "SecondaryStorage"}
    response = self.http_get(command, args=args)
    logger.debug(response)
    return response

  def listStoragePools(self):
    command = "listStoragePools"
    logger.debug("command=" + command)
    response = self.http_get(command)
    logger.debug(response)
    return response

  def createStoragePool(self, name, url, clusterid, zoneid, podid=None):
    command = "createStoragePool"
    logger.debug("command=" + command)
    args = {
             "name": name,
             "url": url,
             "clusterid": clusterid,
             "zoneid": zoneid
           }
    if podid is not None:
      args["podid"] = podid

    response = self.http_get(command, args=args)
    logger.debug(response)
    return response["storagepool"]

  def enableStorageMaintenance(self, storageid):
    command = "enableStorageMaintenance"
    logger.debug("command=" + command)
    args = {"id": storageid}
    response = self.http_get(command, args=args, async=True)
    logger.debug(response)
    return response["storagepool"]

  def cancelStorageMaintenance(self, storageid):
    command = "cancelStorageMaintenance"
    logger.debug("command=" + command)
    args = {"id": storageid}
    response = self.http_get(command, args=args, async=True)
    logger.debug(response)
    return response["storagepool"]

  def deleteStoragePool(self, storageid):
    command = "deleteStoragePool"
    logger.debug("command=" + command)
    args = {"id": storageid}
    response = self.http_get(command, args=args)
    logger.debug(response)
    return response
